🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / core / ble / src / androidMain / kotlin / org / meshtastic / core / ble / KablePlatformSetup.kt
Displaying Raw • Download
core/ble/src/androidMain/kotlin/org/meshtastic/core/ble/KablePlatformSetup.kt 01cd54907d62cd90913d3d071b6bc240cae365ef (01cd5490) Text, 4.30 KB
T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.ble
Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787com.juul.kable.AndroidPeripheral
Tff7b72import T7ee787com.juul.kable.Peripheral
Tff7b72import T7ee787com.juul.kable.PeripheralBuilder
Tff7b72import T7ee787com.juul.kable.PooledThreadingStrategy
Tff7b72import T7ee787com.juul.kable.toIdentifier
Tff7b72import T7ee787org.meshtastic.core.model.util.anonymize
T8b949e/**
* Shared thread pool for Kable BLE connections.
*
* [PooledThreadingStrategy] reuses handler threads across reconnect cycles, avoiding the overhead of creating a new
* thread per connection attempt that [OnDemandThreadingStrategy][com.juul.kable.OnDemandThreadingStrategy] incurs. Idle
* threads are evicted after 1 minute (default).
*
* A single app-wide instance is used because Kable recommends exactly one pool per application.
*/
Tff7b72private Tff7b72val Te6edf3sharedThreadingStrategy Tff7b72= Te6edf3PooledThreadingStrategyTb4b4b4(Tb4b4b4)
Tff7b72internal Tff7b72actual Tff7b72fun Te6edf3PeripheralBuilderTb4b4b4.Td2a8ffplatformConfigTb4b4b4(Te6edf3deviceTb4b4b4: Te6edf3BleDeviceTb4b4b4, Te6edf3autoConnectTb4b4b4: Tb4b4b4(Tb4b4b4) Tff7b72-Tff7b72> Tffa657BooleanTb4b4b4) Tb4b4b4{
T8b949e// Bonded devices without a fresh advertisement must use autoConnect = true. Otherwise,
T8b949e// Android's direct connect algorithm often fails with GATT 133 or times out, especially
T8b949e// if the device uses random resolvable addresses. Scanned devices (advertisement != null)
T8b949e// use direct connection (autoConnect = false) for faster initial connects.
Te6edf3autoConnectIfTb4b4b4(Te6edf3autoConnectTb4b4b4)
Te6edf3threadingStrategy Tff7b72= Te6edf3sharedThreadingStrategy
T8b949e// We intentionally keep Kable's defaults for `transport` (Le) and `phy` (Le1M).
T8b949e// Meshtastic radios (nRF52, ESP32-S3, RP2040+nRF) advertise BLE-only and don't support
T8b949e// the LE 2M PHY in any first-party firmware, so changing these would be a regression risk
T8b949e// with no upside. If a future hardware revision exposes 2M PHY, override `phy = Phy.Le2M`
T8b949e// here after confirming the firmware advertises it.
Te6edf3onServicesDiscovered Tb4b4b4{
Tff7b72try Tb4b4b4{
T8b949e// Android defaults to 23 bytes MTU. Meshtastic packets can be 512 bytes.
T8b949e// Requesting the max MTU is critical for preventing dropped packets and stalls.
Tf0883e@SuppressTb4b4b4(Ta5d6ff"Ta5d6ffMagicNumberTa5d6ff"Tb4b4b4)
Tff7b72val Te6edf3negotiatedMtu Tff7b72= Te6edf3requestMtuTb4b4b4(T79c0ff5T79c0ff1T79c0ff2Tb4b4b4)
Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{ Ta5d6ff"Ta5d6ff[Tffd700${Te6edf3deviceTb4b4b4.Te6edf3addressTb4b4b4.Te6edf3anonymizeTb4b4b4(Tb4b4b4)Tffd700}Ta5d6ff] Negotiated MTU: Tffd700$Te6edf3negotiatedMtuTa5d6ff" Tb4b4b4}
Tb4b4b4} Tff7b72catch Tb4b4b4(Tf0883e@SuppressTb4b4b4(Ta5d6ff"Ta5d6ffTooGenericExceptionCaughtTa5d6ff"Tb4b4b4) Te6edf3eTb4b4b4: Te6edf3ExceptionTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ff[Tffd700${Te6edf3deviceTb4b4b4.Te6edf3addressTb4b4b4.Te6edf3anonymizeTb4b4b4(Tb4b4b4)Tffd700}Ta5d6ff] Failed to request MTUTa5d6ff" Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tff7b72internal Tff7b72actual Tff7b72fun Td2a8ffcreatePeripheralTb4b4b4(Te6edf3addressTb4b4b4: Tffa657StringTb4b4b4, Te6edf3builderActionTb4b4b4: Te6edf3PeripheralBuilderTb4b4b4.Tb4b4b4(Tb4b4b4) Tff7b72-Tff7b72> Tffa657UnitTb4b4b4)Tb4b4b4: Te6edf3Peripheral Tff7b72=
Te6edf3comTb4b4b4.Te6edf3juulTb4b4b4.Te6edf3kableTb4b4b4.Te6edf3PeripheralTb4b4b4(Te6edf3addressTb4b4b4.Te6edf3toIdentifierTb4b4b4(Tb4b4b4)Tb4b4b4, Te6edf3builderActionTb4b4b4)
T8b949e/** ATT protocol header size (opcode + handle) subtracted from MTU to get the usable payload. */
Tff7b72private Tff7b72const Tff7b72val Te6edf3ATT_HEADER_SIZE Tff7b72= T79c0ff3
Tff7b72internal Tff7b72actual Tff7b72fun Te6edf3PeripheralTb4b4b4.Td2a8ffnegotiatedMaxWriteLengthTb4b4b4(Tb4b4b4)Tb4b4b4: Tffa657Int? Tb4b4b4{
Tff7b72val Te6edf3mtu Tff7b72= Tb4b4b4(Tff7b72this Tff7b72as? Te6edf3AndroidPeripheralTb4b4b4)Tff7b72?.Te6edf3mtuTff7b72?.Te6edf3value Tff7b72?: Tff7b72return Tff7b72null
Tff7b72return Tb4b4b4(Te6edf3mtu Tff7b72- Te6edf3ATT_HEADER_SIZETb4b4b4)Tb4b4b4.Te6edf3takeIf Tb4b4b4{ Tffa657it Tff7b72> T79c0ff0 Tb4b4b4}
Tb4b4b4}
Tff7b72internal Tff7b72actual Tff7b72fun Te6edf3PeripheralTb4b4b4.Td2a8ffrequestHighConnectionPriorityTb4b4b4(Tb4b4b4)Tb4b4b4: Tffa657Boolean Tb4b4b4{
Tff7b72val Te6edf3androidPeripheral Tff7b72= Tff7b72this Tff7b72as? Te6edf3AndroidPeripheral Tff7b72?: Tff7b72return Tff7b72false
Tff7b72return Te6edf3runCatching Tb4b4b4{ Te6edf3androidPeripheralTb4b4b4.Te6edf3requestConnectionPriorityTb4b4b4(Te6edf3AndroidPeripheralTb4b4b4.Te6edf3PriorityTb4b4b4.Te6edf3HighTb4b4b4) Tb4b4b4}
Tb4b4b4.Te6edf3onFailure Tb4b4b4{ Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Tffa657itTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffrequestConnectionPriority(High) threwTa5d6ff" Tb4b4b4} Tb4b4b4}
Tb4b4b4.Te6edf3getOrDefaultTb4b4b4(Tff7b72falseTb4b4b4)
Tb4b4b4}
Tff7b72internal Tff7b72actual Tff7b72fun Te6edf3PeripheralTb4b4b4.Td2a8ffrequestBalancedConnectionPriorityTb4b4b4(Tb4b4b4)Tb4b4b4: Tffa657Boolean Tb4b4b4{
Tff7b72val Te6edf3androidPeripheral Tff7b72= Tff7b72this Tff7b72as? Te6edf3AndroidPeripheral Tff7b72?: Tff7b72return Tff7b72false
Tff7b72return Te6edf3runCatching Tb4b4b4{ Te6edf3androidPeripheralTb4b4b4.Te6edf3requestConnectionPriorityTb4b4b4(Te6edf3AndroidPeripheralTb4b4b4.Te6edf3PriorityTb4b4b4.Te6edf3BalancedTb4b4b4) Tb4b4b4}
Tb4b4b4.Te6edf3onFailure Tb4b4b4{ Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Tffa657itTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffrequestConnectionPriority(Balanced) threwTa5d6ff" Tb4b4b4} Tb4b4b4}
Tb4b4b4.Te6edf3getOrDefaultTb4b4b4(Tff7b72falseTb4b4b4)
Tb4b4b4}
Served by rngit 1.5.0 - Generated in 0.11s